package com.kevin.epacms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevin.epacms.constant.CommonConstant;
import com.kevin.epacms.entity.Leave;
import com.kevin.epacms.entity.Record;
import com.kevin.epacms.entity.User;
import com.kevin.epacms.entity.dto.LeaveDto;
import com.kevin.epacms.enums.RoleEnum;
import com.kevin.epacms.mapper.LeaveMapper;
import com.kevin.epacms.mapper.UserMapper;
import com.kevin.epacms.service.LeaveService;
import com.kevin.epacms.util.PageList;
import com.kevin.epacms.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2022-11-10
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Result deleteLeave(Long id) {
        return removeById(id) ? Result.ok(CommonConstant.SUCCESS) : Result.fail(CommonConstant.FAIL);
    }

    @Override
    public Result deleteBatch(String idsStr) {
        List<Long> ids = Arrays.stream(idsStr.split(StrUtil.COMMA)).map(Long::parseLong).collect(Collectors.toList());
        return removeBatchByIds(ids) ? Result.ok(CommonConstant.SUCCESS) : Result.fail(CommonConstant.FAIL);
    }

    @Override
    public Result addOrUpdateLeave(Leave leave) {
        // 如果返校时间早于离校时间则报错
        if(Objects.isNull(leave.getId())){
            if(leave.getReturnTime().isBefore(leave.getLeaveTime())){
                return Result.fail("返校时间不能早于离校时间");
            }
            leave.setApplyTime(LocalDateTime.now());
        }
        return saveOrUpdate(leave) ? Result.ok(CommonConstant.SUCCESS) : Result.fail(CommonConstant.FAIL);
    }

    /**
     *  1管理员 ->查询所有用户的离校记录
     *  2辅导员 -> 查询所管理的学生的离校记录
     *  3学生，4教职工 -> 只查询本人的离校记录
     * @return {@link Result}
     */
    @Override
    public Result getLeaveBySearch(Integer currentPage, Integer pageSize, LeaveDto leaveDto) {
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        if(RoleEnum.COUNSELOR.getCode().equals(leaveDto.getRid())){

        }
        if(RoleEnum.STUDENT.getCode().equals(leaveDto.getRid()) ||
                RoleEnum.TEACHER.getCode().equals(leaveDto.getRid())){
            queryWrapper.eq("uid", leaveDto.getUid());
        }
        IPage<Leave> recordByCondition = baseMapper.selectLeavePage(new Page<>(currentPage, pageSize), queryWrapper);
        PageList recordPageList = new PageList(recordByCondition);
        return Result.ok(recordPageList);
    }
}
